package machine;

import java.awt.Container;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.text.JTextComponent;

class RegisterGUI extends JPanel
    implements ActionListener
{

    private JTextField regDisplay;
    private JComboBox displayOptions;
    private String curDisplay;

    public RegisterGUI(String s)
    {
        curDisplay = "Signed";
        ((Container)this).setLayout(((java.awt.LayoutManager) (new BoxLayout(((Container) (this)), 0))));
        JLabel jlabel = new JLabel(s);
        ((JComponent) (jlabel)).setFont(new Font("Monospaced", 1, 10));
        ((Container)this).add(((java.awt.Component) (jlabel)));
        regDisplay = new JTextField("0", 33);
        ((JTextComponent) (regDisplay)).setEditable(false);
        regDisplay.setFont(new Font("Monospaced", 0, 10));
        ((Container)this).add(((java.awt.Component) (regDisplay)));
        String as[] = {
            "Signed", "Unsigned", "Binary", "Hexadecimal"
        };
        displayOptions = new JComboBox(((Object []) (as)));
        ((JComponent) (displayOptions)).setFont(new Font("Monospaced", 0, 10));
        displayOptions.addActionListener(((ActionListener) (this)));
        ((Container)this).add(((java.awt.Component) (displayOptions)));
    }

    public void set(String s)
    {
        String s1 = (String)displayOptions.getSelectedItem();
        s = paddTo32(s);
        String s2;
        if(s1.equals("Signed"))
        {
            Long long1 = Long.valueOf(s, 2);
            s2 = (new Integer((int)long1.longValue())).toString();
        } else
        if(s1.equals("Unsigned"))
        {
            Long long2 = Long.valueOf(s, 2);
            s2 = long2.toString();
        } else
        if(s1.equals("Binary"))
        {
            s2 = s;
        } else
        if(s1.equals("Hexadecimal"))
        {
            Long long3 = Long.valueOf(s, 2);
            s2 = Long.toHexString(long3.longValue());
            s2 = "0x" + s2.toUpperCase();
        } else
        {
            s2 = "Crap!";
        }
        ((JTextComponent) (regDisplay)).setText(s2);
        curDisplay = s1;
    }

    public void actionPerformed(ActionEvent actionevent)
    {
        String s1 = ((JTextComponent) (regDisplay)).getText();
        String s;
        if(curDisplay.equals("Signed") || curDisplay.equals("Unsigned"))
        {
            Long long1 = Long.valueOf(s1);
            s = Long.toBinaryString(long1.longValue());
        } else
        if(curDisplay.equals("Binary"))
        {
            s = s1;
        } else
        if(curDisplay.equals("Hexadecimal"))
        {
            Long long2 = Long.valueOf(s1.substring(2, s1.length()), 16);
            s = Long.toBinaryString(long2.longValue());
        } else
        {
            s = "Crap!";
        }
        set(s);
    }

    private String paddTo32(String s)
    {
        if(s.length() > 32)
        {
            s = s.substring(s.length() - 32, s.length());
        } else
        {
            for(; s.length() < 32; s = "0" + s) { }
        }
        return s;
    }

}
